package com.goldrepo.play.config;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Description: 数据加载</p>
 *
 * @author Goldrepo
 * @date 2022-05-11 19:02
 **/
@Component
public class CacheLoader {
    private static final Logger logger = LoggerFactory.getLogger(CacheLoader.class);
    public static Map<String, String> contents = new HashMap<>();

    @PostConstruct
    public void init() {
        // 读取文件
        InputStream inputStream = null;
        FileInputStream fileInputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            // 打包成jar后没法读取
            // File file = ResourceUtils.getFile("classpath:data.txt");
            ClassPathResource resource = new ClassPathResource("data.txt");
            inputStream = resource.getInputStream();

            // if(file.isFile() && file.exists()){
            if(resource.isReadable()){
                // fileInputStream = new FileInputStream(file);
                // inputStreamReader = new InputStreamReader(fileInputStream,"utf-8");

                inputStreamReader = new InputStreamReader(inputStream,"utf-8");
                bufferedReader = new BufferedReader(inputStreamReader);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    if(StringUtils.isNotBlank(lineTxt) && !lineTxt.startsWith("#") && lineTxt.contains("--")){
                        String[] split = lineTxt.split("--");
                        contents.put(split[0], split[1]);
                    }
                }
                logger.info("文件加载完毕!!!");
            }else{
                logger.error("找不到data.txt文件默认使用代码里面的卦象");
            }
        } catch (FileNotFoundException e) {
            // e.printStackTrace();
            logger.error("读取data.txt文件异常, 默认使用代码里面的卦象", e);
        } catch (UnsupportedEncodingException e) {
            // e.printStackTrace();
            logger.error("读取data.txt文件异常, 默认使用代码里面的卦象", e);
        } catch (IOException e) {
            // e.printStackTrace();
            logger.error("读取data.txt文件异常, 默认使用代码里面的卦象", e);
        } finally {
            try{
                if(bufferedReader != null){
                    bufferedReader.close();
                }
            } catch (IOException e) {
                logger.error("文件关闭异常", e);
            }
            try{
                if(inputStreamReader != null){
                    inputStreamReader.close();
                }
            } catch (IOException e) {
                logger.error("文件关闭异常", e);
            }
            try{
                if(fileInputStream != null){
                    fileInputStream.close();
                }
            } catch (IOException e) {
                logger.error("文件关闭异常", e);
            }
            try{
                if(inputStream != null){
                    inputStream.close();
                }
            } catch (IOException e) {
                logger.error("文件关闭异常", e);
            }
        }
    }
}
